package model;

import javax.bluetooth.RemoteDevice;

/**
 * 
 * @author conta_usuario
 * 
 */
public class Envelope {
	private RemoteDevice leftBT;
	private RemoteDevice rightBT;
	private boolean disponivelLeft;
	private boolean disponivelRight;

	public Envelope() {
		leftBT = null;
		rightBT = null;
		disponivelLeft = true;
		disponivelRight = true;
	}

	public synchronized void setLeft(RemoteDevice bt) {
		while (disponivelLeft == true) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		leftBT = bt;
		disponivelLeft = true;
		notify();
	}

	public synchronized RemoteDevice getLeft() {
		while (disponivelLeft == false) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		disponivelLeft = false;
		notify();
		return leftBT;
	}

	public synchronized void setRight(RemoteDevice bt) {
		while (disponivelRight == true) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		rightBT = bt;
		disponivelRight = true;
		notify();
	}

	public synchronized RemoteDevice getRight() {
		while (disponivelRight == false) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		disponivelRight = false;
		notify();
		return rightBT;
	}
}